package cn.th.service.impl;

import cn.th.entity.Admin;
import cn.th.entity.Dormitory;
import cn.th.mapper.DormitoryMapper;
import cn.th.service.IDormitoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 宿舍表 服务实现类
 * </p>
 *
 * @author cn.th
 * @since 2024-03-30
 */
@Service
public class DormitoryServiceImpl extends ServiceImpl<DormitoryMapper, Dormitory> implements IDormitoryService {

    @Autowired
    private IDormitoryService dormitoryService;

    @Autowired
    private DormitoryMapper dormitoryMapper;

    /**
     * 新增宿舍，采用excel表格的形式导入
     * @param file
     * @return
     */
    @Transactional
    @Override
    public ResponseEntity<String> saveFile(MultipartFile file) {
        try {
            // 读取Excel文件
            InputStream inputStream = file.getInputStream();
            Workbook workbook = WorkbookFactory.create(inputStream);
            Sheet sheet = workbook.getSheetAt(0);

            // 遍历Excel表格数据,从第二行开始读
            for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
                Row row = sheet.getRow(i);

                String school = row.getCell(0).getStringCellValue(); // 获取每行的第一个单元格中的内容

                String building = row.getCell(1).getStringCellValue(); // 获取每行的第二个单元格中的内容

                double cellValue = row.getCell(2).getNumericCellValue();
                DecimalFormat df = new DecimalFormat("#");
                String number = df.format(cellValue); // 获取每行的第三个单元格中的内容

                Integer total = (int) row.getCell(3).getNumericCellValue(); // 获取每行的第四个单元格中的内容

                // 进行操作，对new出来的Dormitory进行赋值
                Dormitory dormitory = new Dormitory();
                dormitory.setBuilding(building);
                dormitory.setNumber(number);
                dormitory.setSchool(school);
                dormitory.setTotal(Integer.valueOf(total));

                // 将数据存入数据库
                dormitoryService.save(dormitory);
            }
            return ResponseEntity.ok("Excel上传成功");
        } catch (IOException | InvalidFormatException e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Excel上传失败");
        }
    }

    /**
     * 查询宿舍
     * @param page
     * @param pageSize
     * @param school
     * @param building
     * @param number
     * @param total
     * @return
     */
    @Override
    public Map<String, Object> queryDormitory(int page, int pageSize, String school, String building, String number, Integer total) {

        //构造分页构造器
        Page<Dormitory> page1 = new Page<>(page,pageSize);

        //构造条件构造器
        LambdaQueryWrapper<Dormitory> wrapper = new LambdaQueryWrapper<>();

        //添加过滤条件
        wrapper.eq(StringUtils.hasLength(building), Dormitory::getBuilding, building);
        wrapper.eq(StringUtils.hasLength(school), Dormitory::getSchool, school);
        wrapper.like(StringUtils.hasLength(number),Dormitory::getNumber,number); //宿舍号支持模糊查询
        wrapper.eq(total!=null,Dormitory::getTotal,total);

        //添加排序条件,根据id升序排
        wrapper.orderByAsc(Dormitory::getId);

        //执行查询
        dormitoryService.page(page1,wrapper);

        Map<String, Object> data = new HashMap<>();
        data.put("total",page1.getTotal());
        data.put("rows",page1.getRecords());

        return data;
    }

    /**
     * 删除宿舍，可批量删除
     * @param ids
     */
    @Transactional
    public void removeBatchByIds(String ids) {
        dormitoryMapper.deleteBatchByIds(ids);
    }

}
